package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import model.Family;
import model.Genus;
import model.Specie;
import control.CFamily;
import control.CGenus;
import control.CSpecies;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JNewSpecie extends javax.swing.JFrame {
	private JLabel lblTitle;
	private JTextField txtScientificName;
	private JTextField txtUsualName;
	private JLabel lblUsualName;
	private JScrollPane jScrollPane1;
	private JButton bttCancel;
	private JButton bttSave;
	private JComboBox cmbGenus;
	private JLabel lblGenus;
	private JTextArea txaObservations;
	private JLabel lblObservations;
	private JLabel lblName;
	
	private ArrayList<Genus> genus;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
		
	public JNewSpecie() {
		super();
		initGUI();
		loadGenus();
		
		bttSave.addActionListener(new ActionListener() 
		{	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				save();
			}
		});
		
		bttCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				close();
			}
		});
	}
	
	public JNewSpecie(Specie specie, final JListSpecie jListSpecie) 
	{
		super();
		initGUI();
		loadGenus();
		
		txtScientificName.setText(specie.getScientificName());
		txtUsualName.setText(specie.getUsualName());
		txtScientificName.setEditable(false);
		txaObservations.setText(specie.getObservations());
		cmbGenus.setSelectedItem(specie.getGenus().getName());
		
		
		bttSave.addActionListener(new ActionListener() 
		{	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				update();
				jListSpecie.loadSpecies();
			}
		});
		
		bttCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				close();
			}
		});
	}
	
	private void save()
	{
		try 
		{
			Specie specie = new Specie();
			specie.setScientificName(txtScientificName.getText());
			specie.setUsualName(txtUsualName.getText());
			specie.setObservations(txaObservations.getText());
			specie.setGenus(genus.get(cmbGenus.getSelectedIndex()));
			
			if(CSpecies.getInstance().create(specie))
			{
				JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso");
				close();
			}
			else
				JOptionPane.showMessageDialog(null, "Erro ao realizar o cadastro");
		} catch (SQLException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		} catch (ClassNotFoundException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		}
	}
	
	private void close()
	{
		this.dispose();
	}
	
	private void update()
	{
		try 
		{
			Specie specie = new Specie();
			specie.setScientificName(txtScientificName.getText());
			specie.setUsualName(txtUsualName.getText());
			specie.setObservations(txaObservations.getText());
			specie.setGenus(genus.get(cmbGenus.getSelectedIndex()));
			
			if(CSpecies.getInstance().update(specie))
			{
				JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso");
				this.close();
			}
			else
				JOptionPane.showMessageDialog(null, "Erro ao realizar o cadastro");
		} catch (SQLException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		} catch (ClassNotFoundException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		}
	
	}
	
	private void loadGenus()
	{
		try {
			genus = CGenus.getInstance().retrieveAll();
			for (Genus g : genus) {
				cmbGenus.addItem(g.getName());
			}
			
		} catch (SQLException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		} catch (ClassNotFoundException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		}
		
	}
	
	private void initGUI() {
		try {
			this.setVisible(true);
			this.setPreferredSize(new java.awt.Dimension(419, 426));
			this.setLayout(null);
			this.setSize(419, 426);
			{
				lblTitle = new JLabel();
				this.add(lblTitle);
				lblTitle.setText("New Specie");
				lblTitle.setBounds(12, 8, 376, 21);
				lblTitle.setHorizontalTextPosition(SwingConstants.CENTER);
				lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
			}
			{
				lblName = new JLabel();
				this.add(lblName);
				lblName.setText("Scientific Name:");
				lblName.setBounds(12, 47, 114, 21);
			}
			{
				lblObservations = new JLabel();
				this.add(lblObservations);
				lblObservations.setText("Observations:");
				lblObservations.setBounds(12, 125, 101, 21);
			}
			{
				txtScientificName = new JTextField();
				this.add(txtScientificName);
				txtScientificName.setBounds(126, 44, 262, 28);
			}
			{
				lblGenus = new JLabel();
				this.add(lblGenus);
				lblGenus.setText("Genus");
				lblGenus.setBounds(12, 282, 43, 21);
			}
			{
				ComboBoxModel cmbFamilyModel = 
						new DefaultComboBoxModel();
				cmbGenus = new JComboBox();
				this.add(cmbGenus);
				cmbGenus.setModel(cmbFamilyModel);
				cmbGenus.setBounds(67, 278, 321, 28);
			}
			{
				bttSave = new JButton();
				this.add(bttSave);
				bttSave.setText("Save");
				bttSave.setBounds(12, 331, 180, 28);
			}
			{
				bttCancel = new JButton();
				this.add(bttCancel);
				bttCancel.setText("Cancel");
				bttCancel.setBounds(204, 331, 186, 28);
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(12, 152, 373, 102);
				{
					txaObservations = new JTextArea();
					jScrollPane1.setViewportView(txaObservations);
					txaObservations.setBounds(12, 195, 373, 59);
				}
			}
			{
				lblUsualName = new JLabel();
				getContentPane().add(lblUsualName);
				lblUsualName.setText("Usual Name:");
				lblUsualName.setBounds(12, 88, 83, 21);
			}
			{
				txtUsualName = new JTextField();
				getContentPane().add(txtUsualName);
				txtUsualName.setBounds(107, 85, 281, 28);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
